//app.js
App({
  onLaunch: function() {
    var that = this
    that.initlogin(); // 初始化登录
    that.checkNewVersion(); //检测版本更新
    wx.getSystemInfo({   //获取手机型号信息
      success: function (res) {
        console.log(res.model)
        that.globalData.platform = res.platform
        that.globalData.model = res.model
        let modelmes = res.model;
        if (modelmes.search('iPhone X') != -1) {
          that.globalData.isIphoneX = true
        }
        let totalTopHeight = 72
        if (res.model.indexOf('iPhone X') !== -1) {
          totalTopHeight = 88
        } else if (res.model.indexOf('iPhone') !== -1) {
          totalTopHeight = 64
        }
        that.globalData.statusBarHeight = res.statusBarHeight
        that.globalData.titleBarHeight = totalTopHeight - res.statusBarHeight
      },
      failure() {
        that.globalData.statusBarHeight = 0
        that.globalData.titleBarHeight = 0
      }
    })
  },

  onShow(e) {
    var that=this
    wx.showShareMenu({  //小程序被二次打开的时候能够获取到一些信息，例如群的标识
      withShareTicket: true
    })
  },
  // 全局数据
  globalData: {
    requestUrl: "https://www.cn-healthcare.com/freezingapi/api/", //全局域名
    globalUserMsg: null,
    globalPhone: null,
    userInfo: '',
    memcard: '',
    isShowAllowBox2: 0,
    newValue:''
  },
  
  // 解析富文本内容
  convertHtmlToText: function convertHtmlToText(inputText) {
    var returnText = "" + inputText;
    returnText = returnText.replace(/<\/div>/ig, '\r\n');
    returnText = returnText.replace(/<\/li>/ig, '\r\n');
    returnText = returnText.replace(/<li>/ig, '  *  ');
    returnText = returnText.replace(/<\/ul>/ig, '\r\n');
    //-- remove BR tags and replace them with line break
    returnText = returnText.replace(/<br\s*[\/]?>/gi, "\r\n");
    //-- remove P and A tags but preserve what's inside of them
    returnText = returnText.replace(/<p.*?>/gi, "\r\n");
    returnText = returnText.replace(/<a.*href="(.*?)".*>(.*?)<\/a>/gi, " $2 ($1)");
    //-- remove all inside SCRIPT and STYLE tags
    returnText = returnText.replace(/<script.*>[\w\W]{1,}(.*?)[\w\W]{1,}<\/script>/gi, "");
    returnText = returnText.replace(/<style.*>[\w\W]{1,}(.*?)[\w\W]{1,}<\/style>/gi, "");
    //-- remove all else
    returnText = returnText.replace(/<(?:.|\s)*?>/g, "");
    //-- get rid of more than 2 multiple line breaks:
    returnText = returnText.replace(/(?:(?:\r\n|\r|\n)\s*){2,}/gim, "\r\n\r\n");
    //-- get rid of more than 2 spaces:
    returnText = returnText.replace(/ +(?= )/g, '');
    //-- get rid of html-encoded characters:
    returnText = returnText.replace(/ /gi, " ");
    returnText = returnText.replace(/&/gi, "&");
    returnText = returnText.replace(/"/gi, '"');
    returnText = returnText.replace(/</gi, '<');
    returnText = returnText.replace(/>/gi, '>');
    return returnText;
  },

  // 初始化登录
  initlogin: function(opt) {
    var isfalse = false;
    var that = this;
    wx.login({
      success: function(res) {
        console.log(res)
        if (res.code) {
          wx.setStorageSync('code', res.code)
          wx.request({
            url: 'https://www.cn-healthcare.com/freezingapi/api/article/rebangQueryUnionid?&js_code=' + res.code + '&grant_type=authorization_code',
            method: 'get',
            dataType: 'json',
            header: {
              "Content-Type": "application/x-www-form-urlencoded",
            },
            success: function(res) {
              console.log(res)
              wx.setStorageSync('openid', res.data.data.data.openid)
              wx.setStorageSync('unionid', res.data.data.data.unionid)
              wx.setStorageSync('session_key', res.data.data.data.session_key)
              //每次初始化都重新获取用户信息
              var unionid = res.data.data.data.unionid;
              console.log(unionid)
              if(unionid){    //判断是否绑定过手机号
                wx.request({
                  url: "https://www.cn-healthcare.com/freezingapi/api/article/rebangWxLogin?unionid=" + unionid,
                  method: "get",
                  dataType: "json",
                  header: {
                    "Content-Type": "application/x-www-form-urlencoded"
                  },
                  success: function (res) {
                    console.log(res.data.code);
                    wx.setStorageSync('appdata',res.data.code) 
                    if (res.data.code == 0) { //已经绑定
                      console.log(wx.getStorageSync('unionid'));
                      wx.setStorageSync("nobind", false);
                      wx.setStorageSync("token", res.data.datas.token);
                      wx.setStorageSync("memcard", res.data.datas.memcard);
                    } 
                  }
                });
              }
            }
          });
          //发起网络请求
        } else {
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }
    });
    if (isfalse) {
      return true;
    }
  },
  checkNewVersion: function() {
    // 获取小程序更新机制兼容
    if (wx.canIUse('getUpdateManager')) {
      const updateManager = wx.getUpdateManager()
      updateManager.onCheckForUpdate(function(res) {
        // 请求完新版本信息的回调
        if (res.hasUpdate) {
          updateManager.onUpdateReady(function() {
            wx.showModal({
              title: '更新提示',
              content: '新版本已经准备好，是否重启应用？',
              success: function(res) {
                if (res.confirm) {
                  // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                  updateManager.applyUpdate()
                }
              }
            })
          })
          updateManager.onUpdateFailed(function() {
            // 新的版本下载失败
            wx.showModal({
              title: '已经有新版本了哟~',
              content: '新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~',
            })
          })
        }
      })
    } else {
      // 如果希望用户在最新版本的客户端上体验您的小程序，可以这样子提示
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }

  }
})